﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADC.Utilites;
using ADC.BL;

namespace ADC.Web.Admin
{
    public partial class ProductType : System.Web.UI.Page
    {
        log4net.ILog logger = log4net.LogManager.GetLogger("ListProductType");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Page.Title = Common.PAGE_TITLE + "Danh muc dieu hoa";
                BindData();
            }
        }

        private void BindData()
        {
            try
            {
                var productTypes = ProductTypeFactory.SelectAll();
                GridViewProgrameType.AutoGenerateColumns = false;
                GridViewProgrameType.DataSource = productTypes;
                GridViewProgrameType.DataBind();
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }

        }

        protected void GridViewProgrameType_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                var lblTypeID = (Label)GridViewProgrameType.Rows[e.NewEditIndex].FindControl("lblTypeID");
                if (lblTypeID != null && !string.IsNullOrEmpty(lblTypeID.Text))
                {
                    Response.Redirect("ManageProductType.aspx?Mode=1&ID=" + lblTypeID.Text, false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }

        protected void GridViewProgrameType_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                var lblTypeID = (Label)GridViewProgrameType.Rows[e.RowIndex].FindControl("lblTypeID");
                if (lblTypeID != null && !string.IsNullOrEmpty(lblTypeID.Text))
                {
                    var programTypeID = int.Parse(lblTypeID.Text);
                    // Check if the current programType does not have program
                    var numberOfProgam = ProductFactory.CountProgram(programTypeID);

                    if (numberOfProgam > 0)
                    {
                        // Show error message
                        Page.RegisterStartupScript("scriptAlert", "<script>alert('Please delete all programs that belong the current program type first!');</script> ");
                        return;
                    }

                    int result = ProductTypeFactory.delete(programTypeID);
                    BindData();
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }

    }
}